package com.niiwoo.civet.mobile.service.bill.detail;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.niiwoo.civet.account.BillBaseDubboService;
import com.niiwoo.civet.account.dto.response.BankInfoRespDTO;
import com.niiwoo.civet.account.dto.response.NiiwooBillRespDTO;
import com.niiwoo.civet.account.dto.response.UserBillBaseResponseDTO;
import com.niiwoo.civet.account.dto.response.WithdrawOrderResponseDTO;
import com.niiwoo.civet.account.enums.BillSecondSubjectEnum;
import com.niiwoo.civet.account.enums.BillStatusEnum;
import com.niiwoo.civet.account.service.WithdrawDubboService;
import com.niiwoo.civet.mobile.vo.response.bill.JumpTypeEnum;
import com.niiwoo.civet.mobile.vo.response.bill.UserBillDetailResponse;
import com.niiwoo.tripod.lanmao.enums.WithdrawTypeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 * 提现账单详情
 * Created by xiaoyingping on 2018/11/7.
 */
@Service
public class WithdrawBillDetailStrategyImpl implements BillDetailStrategy {

    @Reference(version = "1.0.0")
    private WithdrawDubboService withdrawDubboService;
    @Reference(version = "1.0.0")
    private BillBaseDubboService billBaseDubboService;

    @Override
    public String build(UserBillBaseResponseDTO userBillBase, List<NiiwooBillRespDTO> userBillFlowList, UserBillDetailResponse response) {
        // 查询提现订单,获取到帐方式withdraw_type和bank_card_id
        WithdrawOrderResponseDTO withdrawOrderResponseDTO = withdrawDubboService.getWithdrawOrderById(Long.valueOf(response.getOrderId()));

        for (UserBillDetailResponse.AmountItem amountItem : response.getAmountItems()) {
            if(BillSecondSubjectEnum.USER_WITHDRAW_SERVICE_FEE.getCode().equals(amountItem.getCode())){
                amountItem.setName("实收手续费(元)");
            }
        }

        if(withdrawOrderResponseDTO.getFee().compareTo(withdrawOrderResponseDTO.getRealFee()) > 0){
            UserBillDetailResponse.AmountItem feeItem = response.new AmountItem();
            feeItem.setName("应收手续费(元)");
            feeItem.setAmountDesc(withdrawOrderResponseDTO.getFee().toString());
            response.getAmountItems().add(feeItem);

            UserBillDetailResponse.AmountItem redBagItem = response.new AmountItem();
            redBagItem.setName("使用卡券抵扣(元)");
            String redBagItemContent = withdrawOrderResponseDTO.getFee().subtract(withdrawOrderResponseDTO.getRealFee()).toString();
            if(BillStatusEnum.CLOSE.getValue() == userBillBase.getStatus()){
                redBagItemContent += "(已返还)";
            }
            redBagItem.setAmountDesc(redBagItemContent);
            response.getAmountItems().add(redBagItem);
        }

        UserBillDetailResponse.SpecialItem specialItem = response.new SpecialItem();
        if(Objects.nonNull(withdrawOrderResponseDTO.getWithdrawType())){
            specialItem.setName("到账方式");
            specialItem.setContent(this.getWithdrawTypeDesc(withdrawOrderResponseDTO.getWithdrawType()));
            response.getSpecialItems().add(specialItem);
        }

        if(Objects.nonNull(withdrawOrderResponseDTO.getBankCardId())){
            // 获取银行名称和银行卡号后4位
            BankInfoRespDTO bankInfoRespDTO = withdrawDubboService.getWithdrawingOrderBankInfo(withdrawOrderResponseDTO.getBankCardId());
            String bankName = bankInfoRespDTO.getBankName();
            String bankNo = StringUtils.right(bankInfoRespDTO.getBankNo(), 4);

            specialItem = response.new SpecialItem();
            specialItem.setName("到账至");
            specialItem.setContent(bankName + "(" + bankNo + ")");
            response.getSpecialItems().add(specialItem);
        }

        //查询提现失败回退的账单
        if(BillStatusEnum.CLOSE == BillStatusEnum.getEnumByCode(userBillBase.getStatus())){
            List<UserBillBaseResponseDTO> billBaseResponseDTOList = billBaseDubboService.queryBackSourceUserBillBases(userBillBase.getUserId(), userBillBase.getBillBaseId());
            if(!CollectionUtils.isEmpty(billBaseResponseDTOList)){
                specialItem = response.new SpecialItem();
                specialItem.setName("退款说明");
                specialItem.setContent("提现失败退回");
                specialItem.setIsNeedJump(Boolean.TRUE);
                specialItem.setJumpType(JumpTypeEnum.BILL_DETAIL.getCode());
                specialItem.setJumpParamOne(billBaseResponseDTOList.get(0).getBillBaseId().toString());
                response.getSpecialItems().add(specialItem);
            }
        }

        return JSON.toJSONString(response);
    }


    private String getWithdrawTypeDesc(Byte withdrawType){
        String desc = "未知";
        if(Objects.nonNull(withdrawType)){
            WithdrawTypeEnum withdrawTypeEnum = WithdrawTypeEnum.enumOf(withdrawType);
            if(WithdrawTypeEnum.NORMAL == withdrawTypeEnum){
                desc = "普通到账";
            }else if(WithdrawTypeEnum.URGENT == withdrawTypeEnum){
                desc = "快速到账";
            }else if(WithdrawTypeEnum.NORMAL_URGENT == withdrawTypeEnum){
                desc = "智能快速到账";
            }
        }

        return desc;
    }
}
